package com.heima.behavior.service.impl;

import com.heima.behavior.dtos.LikesBehaviorDto;
import com.heima.behavior.dtos.UnLikesBehaviorDto;
import com.heima.behavior.pojos.ApLikesBehavior;
import com.heima.behavior.pojos.ApUnlikesBehavior;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.behavior.service.ApUnLikesBehaviorService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.AppThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUnLikesBehaviorServiceImpl implements ApUnLikesBehaviorService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private StringRedisTemplate redisTemplate;


    @Override
    public ResponseResult unlike(UnLikesBehaviorDto dto) {

        //1、校验参数
        if (dto.getArticleId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2、获取登入人信息
        ApUser apUser = AppThreadLocalUtil.getApUser();
        if (apUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        //2.判断用户是否已经不喜欢 ，查询redis
        //2.1 获取关注缓存hash类型的key
        String Unlike_hash_key="USER_UNLIKES_"+dto.getArticleId();
        //2.3判断是已经点击不喜欢 ，查询关注缓存中的小key是否包含点赞人id
        Boolean hasKeyFlag = redisTemplate.opsForHash().hasKey(Unlike_hash_key, apUser.getId()+"");


        //3.用户未点击 不喜欢
        if(!hasKeyFlag && dto.getType()==0){//要执行点赞行为操作
            //保存数据到redis中
            redisTemplate.opsForHash().put(Unlike_hash_key,apUser.getId()+"","1");
            //保存数据到mongo中
            ApUnlikesBehavior apLikesBehavior = new ApUnlikesBehavior();
            apLikesBehavior.setType(dto.getType());
            apLikesBehavior.setArticleId(dto.getArticleId());
            apLikesBehavior.setUserId(apUser.getId().longValue());
            apLikesBehavior.setCreatedTime(new Date());
            mongoTemplate.insert(apLikesBehavior);
            return ResponseResult.okResult(3006,"点击不喜欢成功");
        }
        //4.用户已点击 不喜欢
        if(hasKeyFlag && dto.getType()==0){//表示已经点赞了，无须任何操作
            return ResponseResult.okResult(3007,"已点击不喜欢");
        }
        if(hasKeyFlag && dto.getType()==1){//表示要执行取消点赞操作
            //3.1 则删除缓存数据
            redisTemplate.opsForHash().delete(Unlike_hash_key,apUser.getId()+"");
            //3.2 删除mongodb中的数据
            Query query= Query.query(Criteria.where("userId").is(apUser.getId())
                    .and("articleId").is(dto.getArticleId()));
            mongoTemplate.remove(query,ApUnlikesBehavior.class);
            return ResponseResult.okResult(3008,"取消成功");
        }
        return null;
    }
}